package middleware

import (
	"github.com/go-kit/kit/log"
	"http_rest/service"
	"time"
)
type loggingMiddleware struct {
	service.Service
	logger log.Logger
}

func LoggingMiddleware(logger log.Logger) service.ServiceMiddleware{
	return func(next service.Service) service.Service{
		return loggingMiddleware{next, logger}
	}
}

func (mw loggingMiddleware) Add(a,b int) (ret int){
	defer func(begin time.Time) {
		mw.logger.Log(
			"function", "Add",
			"a", a,
			"b", b,
			"result", ret,
			"took", time.Since(begin),
			)
	}(time.Now())
	ret = mw.Service.Add(a, b)
	return  ret
}